package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.Task;
import com.qidianit.mes.pojo.WorkReport;
import com.qidianit.mes.pro.mapper.*;
import com.qidianit.mes.pro.service.WorkReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Description:
 * @ Author:WuJingLei
 * @ Date: 2025/9/26 -17:53
 */
@Service
public class WorkReportServiceImpl implements WorkReportService {
    @Autowired
    private WorkReportMapper workReportMapper;
    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private ProcessMapper processMapper;
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private UnitMapper unitMapper;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public Result<PageInfo<WorkReport>> queryWorkReports(WorkReport workReport, int page, int size) {
        //分页
        PageHelper.startPage(page,size);
        //查询数据
        List<WorkReport> workReports = workReportMapper.queryWorkReportList(workReport);
        for (WorkReport report : workReports) {
            report.setTypeDictData(dictDataMapper.queryByDictDataId(report.getType()));
            report.setStatusDictData(dictDataMapper.queryByDictDataId(report.getStatus()));
            report.setSubmitUser(sysUserMapper.getUserById(report.getSubmitBy()));
            report.setCheckUser(sysUserMapper.getUserById(report.getCheckBy()));
            Task task = taskMapper.queryByTaskCode(report.getTaskCode());
            report.setProcess(processMapper.queryById(task.getProcessId()));
            report.setMaterial(materialMapper.queryByCode(report.getMaterialCode()));
            report.getMaterial().setUnit(unitMapper.queryById(report.getMaterial().getUnitId()));
        }
        return Result.success(new PageInfo<>(workReports));
    }

    @Override
    public Result<WorkReport> getById(Long id) {
        WorkReport workReport = workReportMapper.queryByWorkReport(id);
        workReport.setTypeDictData(dictDataMapper.queryByDictDataId(workReport.getType()));
        workReport.setStatusDictData(dictDataMapper.queryByDictDataId(workReport.getStatus()));
        workReport.setSubmitUser(sysUserMapper.getUserById(workReport.getSubmitBy()));
        workReport.setCheckUser(sysUserMapper.getUserById(workReport.getCheckBy()));
        Task task = taskMapper.queryByTaskCode(workReport.getTaskCode());
        workReport.setProcess(processMapper.queryById(task.getProcessId()));
        workReport.setMaterial(materialMapper.queryByCode(workReport.getMaterialCode()));
        workReport.getMaterial().setUnit(unitMapper.queryById(workReport.getMaterial().getUnitId()));
        return Result.success(workReport);
    }

    @Override
    public Result<WorkReport> saveWorkReport(WorkReport workReport) {

        return workReportMapper.addWorkReport(workReport)>0?Result.success(workReport):Result.error("报工添加失败");
    }

    @Override
    public Result deleteById(Long id) {
        return workReportMapper.deleteWorkReport(id)>0?Result.success():Result.error("删除报工失败");
    }

    @Override
    public Result batchDelete(Long[] id) {
        return workReportMapper.batchDeleteWorkReports(id)>0?Result.success():Result.error("批量删除失败");
    }

    @Override
    public Result updateWorkReport(WorkReport workReport) {
        return workReportMapper.updateWorkReport(workReport)>0?Result.success():Result.error("报工更新失败");
    }
}
